Imports System.ComponentModel
Imports System.Runtime.Serialization

Partial Class Store
    Inherits BaseBusiness

   
End Class


Partial Class Customer
    Inherits BaseBusiness

    ''' <summary>
    ''' Called when the object is deserialized from the service in 
    ''' order to hook back up the PropertyChanged event handler.
    ''' </summary>
    ''' <param name="context"></param>
    ''' <remarks></remarks>
    <OnDeserialized()> _
    Private Sub OnDeserialized(ByVal context As StreamingContext)
        AddHandler Me.PropertyChanged, AddressOf MyPropertyChanged
    End Sub

    Private Sub MyPropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Handles Me.PropertyChanged
        Me.IsDirty = True
    End Sub

    Public Overrides Sub Validate()
        Me.OnValidate(System.Data.Linq.ChangeAction.None)
    End Sub

    Private Sub OnNameChanging(ByVal value As String)
        Me.CheckName(value)
    End Sub

    Private Sub OnUserNameChanging(ByVal value As String)
        Me.CheckUserName(value)
    End Sub

    Private Sub OnValidate(ByVal action As System.Data.Linq.ChangeAction)

        'TODO: Add Phone, Zip, etc formatting/validation.

        Me.CheckName(Me.Name)
        Me.CheckUserName(Me.UserName)

        If Me.HasErrors Then
            Throw New ValidationException(Me.Error)
        End If
    End Sub

    Private Sub CheckName(ByRef value As String)
        If String.IsNullOrEmpty(value) Then
            Me.AddError("Name", "Customer name cannot be empty.")
        Else
            Me.RemoveError("Name")
            'value = value.ToUpper 'add formatting here if necessary
        End If
    End Sub

    Private Sub CheckUserName(ByRef value As String)
        If String.IsNullOrEmpty(value) Then
            Me.AddError("UserName", "User name cannot be empty.")
        Else
            Me.RemoveError("UserName")
            'value = value.ToUpper 'add formatting here if necessary
        End If
    End Sub

    'Pattern for formatting input without validation
    'Private Sub OnNameChanged()
    '    Me._Name = Me.Name.ToUpper
    'End Sub
End Class

Partial Class Order
    Inherits BaseBusiness

    ''' <summary>
    ''' Called when the object is deserialized from the service in 
    ''' order to hook back up the PropertyChanged event handler.
    ''' </summary>
    ''' <param name="context"></param>
    ''' <remarks></remarks>
    <OnDeserialized()> _
    Private Sub OnDeserialized(ByVal context As StreamingContext)
        AddHandler Me.PropertyChanged, AddressOf MyPropertyChanged
    End Sub

    Private Sub MyPropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Handles Me.PropertyChanged
        If e.PropertyName <> "Customer" Then
            Me.IsDirty = True
        End If
    End Sub

    Public Overrides Sub Validate()
        Me.OnValidate(System.Data.Linq.ChangeAction.None)

        'Validate the OrderPizzas if there are any
        For Each p In Me.OrderPizzas
            p.Validate()
        Next
    End Sub

    Private Sub OnCustomer_IDChanging(ByVal value As Integer)
        Me.CheckCustomerID(value)
    End Sub

    Private Sub OnStore_IDChanging(ByVal value As Integer)
        Me.CheckStoreID(value)
    End Sub

    Private Sub OnOrderDateChanging(ByVal value As Date?)
        Me.CheckOrderDate(value)
    End Sub

    Private Sub OnDeliveredDateChanging(ByVal value As Date?)
        Me.CheckDeliveredDate(value)
    End Sub

    Private Sub OnValidate(ByVal action As System.Data.Linq.ChangeAction)
        Me.CheckCustomerID(Me.Customer_ID)
        Me.CheckStoreID(Me.Store_ID)
        Me.CheckOrderDate(Me.OrderDate)
        Me.CheckDeliveredDate(Me.DeliveredDate)

        If Me.HasErrors Then
            Throw New ValidationException(Me.Error)
        End If
    End Sub

    Private Sub CheckStoreID(ByVal value As Integer)
        If value < 1 Then
            Me.AddError("Store_ID", "Store must be specified.")
        Else
            Me.RemoveError("Store_ID")
        End If
    End Sub

    Private Sub CheckCustomerID(ByVal value As Integer)
        If value < 1 Then
            Me.AddError("Customer_ID", "Customer must be specified.")
        Else
            Me.RemoveError("Customer_ID")
        End If
    End Sub

    Private Sub CheckOrderDate(ByVal value As Date?)
        If value.HasValue Then
            If value > Me.DeliveredDate Then
                Me.AddError("OrderDate", "Order date cannot be after the ship date.")
            Else
                Me.RemoveError("OrderDate")
            End If
        End If
    End Sub

    Private Sub CheckDeliveredDate(ByVal value As Date?)
        If value.HasValue Then
            If value < Me.OrderDate Then
                Me.AddError("ShipDate", "Ship date cannot be before the order date.")
            Else
                Me.RemoveError("ShipDate")
            End If
        End If
    End Sub

    Public Overrides ReadOnly Property HasErrors() As Boolean
        Get
            If Not MyBase.HasErrors Then
                'Returns True if any order details are invalid 
                For Each detail In Me.OrderPizzas
                    If detail.HasErrors Then
                        Return True
                    End If
                Next
            End If
            Return MyBase.HasErrors
        End Get
    End Property

   
End Class

Partial Class OrderPizza
    Inherits BaseBusiness

    
End Class

Partial Class OrderPizzaTopping
    Inherits BaseBusiness

End Class

Partial Class Pizza
    Inherits BaseBusiness

    
End Class

Partial Class Topping
    Inherits BaseBusiness

   
End Class